package com.zzf.recyclerviewtest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val fruitList = ArrayList<Fruit>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initFruits()
        val layoutManager = StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)
//        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        recyclerView.layoutManager = layoutManager
        val adapter = FruitAdapter(fruitList)
        recyclerView.adapter = adapter
    }


    /*初始化列表数据*/
    private fun initFruits(){
        repeat(2){
            fruitList.add(Fruit(getRandomLengthString("apple_pic"),R.mipmap.apple_pic))
            fruitList.add(Fruit(getRandomLengthString("banana_pic"),R.mipmap.banana_pic))
            fruitList.add(Fruit(getRandomLengthString("orange_pic"),R.mipmap.orange_pic))
            fruitList.add(Fruit(getRandomLengthString("watermelon_pic"),R.mipmap.watermelon_pic))
            fruitList.add(Fruit(getRandomLengthString("pear_pic"),R.mipmap.pear_pic))
            fruitList.add(Fruit(getRandomLengthString("grape_pic"),R.mipmap.grape_pic))
            fruitList.add(Fruit(getRandomLengthString("pineapple_pic"),R.mipmap.pineapple_pic))
            fruitList.add(Fruit(getRandomLengthString("strawberry_pic"),R.mipmap.strawberry_pic))
            fruitList.add(Fruit(getRandomLengthString("cherry_pic"),R.mipmap.cherry_pic))
            fruitList.add(Fruit(getRandomLengthString("mango_pic"),R.mipmap.mango_pic))
        }
    }

    private fun getRandomLengthString(str:String):String{
        val n = (1..20).random()
        val builder = StringBuilder()
        repeat(n){
            builder.append(str)
        }
        return builder.toString()
    }
}